import os
import shutil
import json
import subprocess

def copy_resources():
    current_dir = os.getcwd()
    
    # 读取已处理文件记录
    record_file = os.path.join(current_dir, 'processed_files.json')
    processed_files = {'xhdpi': [], 'xxhdpi': []}
    if os.path.exists(record_file):
        with open(record_file, 'r', encoding='utf-8') as f:
            processed_files = json.load(f)
    
    # 源目录
    source_xhdpi = os.path.join(current_dir, 'xhdpi')
    source_xxhdpi = os.path.join(current_dir, 'xxhdpi')
    
    # 目标目录
    target_xhdpi = r'D:\Project\ShuZhiYuanProject\shuzhiyuan_android\kunLunFuleCard\src\main\res\drawable-xhdpi'
    target_xxhdpi = r'D:\Project\ShuZhiYuanProject\shuzhiyuan_android\kunLunFuleCard\src\main\res\drawable-xxhdpi'
    
    # 确保目标目录存在
    os.makedirs(target_xhdpi, exist_ok=True)
    os.makedirs(target_xxhdpi, exist_ok=True)
    
    # 复制 xhdpi 文件
    if os.path.exists(source_xhdpi):
        for file in os.listdir(source_xhdpi):
            # 检查文件是否已处理
            file_record = next((record for record in processed_files['xhdpi'] if record['filename'] == file), None)
            if file_record and file_record['status'] == 'copied':
                print(f'跳过已处理文件: {file}')
                continue
            # 如果文件是重命名后的文件，不跳过
            if file_record and file_record['status'] == 'renamed':
                print(f'处理重命名后的文件: {file}')
                
            source_file = os.path.join(source_xhdpi, file)
            target_file = os.path.join(target_xhdpi, file)
            try:
                shutil.copy2(source_file, target_file)
                print(f'已复制: {source_file} -> {target_file}')
                # 添加文件到git
                try:
                    subprocess.run(['git', 'add', target_file], check=True)
                    print(f'已添加到git: {target_file}')
                except subprocess.CalledProcessError as e:
                    print(f'git add 失败: {target_file}')
                    print(f'错误信息: {str(e)}')
            except Exception as e:
                print(f'复制失败: {source_file}')
                print(f'错误信息: {str(e)}')
    
    # 复制 xxhdpi 文件
    if os.path.exists(source_xxhdpi):
        for file in os.listdir(source_xxhdpi):
            # 检查文件是否已处理
            file_record = next((record for record in processed_files['xxhdpi'] if record['filename'] == file), None)
            if file_record and file_record['status'] == 'copied':
                print(f'跳过已处理文件: {file}')
                continue
            # 如果文件是重命名后的文件，不跳过
            if file_record and file_record['status'] == 'renamed':
                print(f'处理重命名后的文件: {file}')
                
            source_file = os.path.join(source_xxhdpi, file)
            target_file = os.path.join(target_xxhdpi, file)
            try:
                shutil.copy2(source_file, target_file)
                print(f'已复制: {source_file} -> {target_file}')
                # 添加文件到git
                # try:
                #     subprocess.run(['git', 'add', target_file], check=True)
                #     print(f'已添加到git: {target_file}')
                # except subprocess.CalledProcessError as e:
                #     print(f'git add 失败: {target_file}')
                #     print(f'错误信息: {str(e)}')
            except Exception as e:
                print(f'复制失败: {source_file}')
                print(f'错误信息: {str(e)}')

if __name__ == '__main__':
    copy_resources()
    print('复制完成！')